package com.powergisol.gis.service.impl;


import com.powergisol.gis.geom.GeoLineString;
import com.powergisol.gis.geom.GeoPolygon;
import com.powergisol.core.math.Dot;
import com.powergisol.core.model.SectionCreater;
import com.powergisol.gis.dto.ModulePoygonDTO;
import com.powergisol.gis.dto.SectionDTO;
import com.powergisol.gis.service.SectionService;
import com.powergisol.gis.support.ModuleResolver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class SectionServiceImpl implements SectionService {

    @Autowired
    ModuleResolver moduleResolver;


    @Override
    public List<SectionDTO> createSection(List<GeoPolygon> sections, String plane) throws IOException{


        List<ModulePoygonDTO> listPolygon = moduleResolver.getListPolygon(plane);

        List<SectionDTO> dtoList = new ArrayList<>();
        sections.forEach(section -> {


            for (int i = 0; i < listPolygon.size(); i++) {

                GeoPolygon polygonUp = listPolygon.get(i).getGeoPolygon();
                int levelUp = listPolygon.get(i).getLevel();
                GeoPolygon polygonDown = listPolygon.get(++i).getGeoPolygon();
                int levelDown = listPolygon.get(i).getLevel();

                List<Dot> dots = new ArrayList<>();

                SectionCreater creater = new SectionCreater();

                List<Dot> dotsUp = creater.getIntersectPoint(section, polygonUp);
                if (!dotsUp.isEmpty()) {
                    dots.addAll(dotsUp);
                    List<Dot> dotsDown = creater.getIntersectPoint(section, polygonDown);
                    dots.addAll(dotsDown);
                }
                /**
                 * 构建4边形
                 */

                if(!dots.isEmpty()){
                    GeoPolygon geoPolygon = new GeoPolygon(dots);
                    SectionDTO sectionDTO = new SectionDTO(geoPolygon,levelUp);
                    dtoList.add(sectionDTO);
                }

            }
        });

        return dtoList;

    }



    @Override
    public List<SectionDTO> createSectionByLine(List<GeoLineString> strings, String plane, double height) throws IOException {

        List<ModulePoygonDTO> listPolygon = moduleResolver.getListPolygon(plane);

        List<SectionDTO> dtoList = new ArrayList<>();

        for (int j = 0; j < strings.size(); j++) {

            for (int i = 0; i < listPolygon.size(); i++) {

                GeoPolygon polygonUp = listPolygon.get(i).getGeoPolygon();
                int levelUp = listPolygon.get(i).getLevel();
                GeoPolygon polygonDown = listPolygon.get(++i).getGeoPolygon();
                int levelDown = listPolygon.get(i).getLevel();

                List<Dot> dots = new ArrayList<>();

                SectionCreater creater = new SectionCreater();

                List<Dot> dotsUp = creater.getIntersectPoint(strings.get(j), polygonUp,height);

                // TODO: 2020/10/25 是不是用三角形表示更好
                if (!dotsUp.isEmpty()) {
                    dots.addAll(dotsUp);
                    List<Dot> dotsDown = creater.getIntersectPoint(strings.get(j), polygonDown,height);
                    /**
                     * 在边上的4边线,需要换下边的顺序,当height起作用的时候
                     */
                    if(!dotsDown.isEmpty()){
                        Dot dotLast = dotsUp.get(dotsUp.size() - 1);
                        Dot dotNext = dotsDown.get(0);

                        if(dotLast.getX()==dotNext.getX()&&dotLast.getY()==dotNext.getY()){
                            dots.addAll(dotsDown);
                        }else {
                            //调换顺序
                            dots.add(dotsDown.get(1));
                            dots.add(dotsDown.get(0));
                        }


                    }else{
                        // TODO: 2020/10/25 当height起作用的时候,这里就要填坑了
                    }

                }
                /**
                 * 构建4边形
                 */
                if(!dots.isEmpty()){
                    GeoPolygon geoPolygon = new GeoPolygon(dots);
                    SectionDTO sectionDTO = new SectionDTO(geoPolygon,levelUp);
                    dtoList.add(sectionDTO);
                }

            }
        }

        return dtoList;
    }



}
